package Service.Student.Imp;

import Dao.DaoFactory;
import Dao.Grade.GradingDao;
import Dao.Grade.GroupGradeDao;
import Dao.Process.AdviserGradeDao;
import Dao.Process.ReportDao;
import Dao.Process.StageCheckDao;
import Dao.Role.ReplyGroupDao;
import Dao.Student.StudentPaperDao;
import Dao.TitleAndPaper.Imp.PaperDaoImp;
import Dao.TitleAndPaper.Imp.TitleDaoImp;
import Entity.Grade.FinalGrade;

import Entity.Process.Report;
import Entity.Process.ReportDetail;

import Entity.ReplyGroup;
import Entity.TitleAndPaper.Paper;

import Service.Student.StudentPaperService;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;


public class StudentPaperServiceImp implements StudentPaperService {
    private StudentPaperDao studentPaperDao;
    private ReportDao reportDao;
    private StageCheckDao stageCheckDao;
    private Connection conn;
    private AdviserGradeDao adviserGradeDao;
    private PaperDaoImp paperDao;
    private TitleDaoImp titleDao;
    private GroupGradeDao groupGradeDao;
    private GradingDao gradingDao;
    private ReplyGroupDao replyGroupDao;

    public StudentPaperServiceImp() {
        studentPaperDao = DaoFactory.getInstance("studentPaperDao");
        reportDao = DaoFactory.getInstance("reportDao");
        stageCheckDao = DaoFactory.getInstance("stageCheckDao");
        adviserGradeDao = DaoFactory.getInstance("adviserGradeDao");
        paperDao = DaoFactory.getInstance("paperDao");
        titleDao = DaoFactory.getInstance("titleDao");
        groupGradeDao = DaoFactory.getInstance("groupGradeDao");
        gradingDao = DaoFactory.getInstance("gradingDao");
        replyGroupDao = DaoFactory.getInstance("replyGroupDao");
        conn = DBUtil.getConnByDruid();
    }

    /**
     * 提交开题报告
     */
    @Override
    public boolean submitReport(Report report, ReportDetail reportDetail) {
        boolean flag = false;
        Report report1 = reportDao.getReportBySNO(report.getSno(), conn);
        try {
            //判断是直接插入开题报告还是修改开题报告
            if (report1 == null) {
                conn.setAutoCommit(false);//取消事务自动提交
                int rs1 = studentPaperDao.insertReport(report, conn);
                //获取刚插入的开题报告的id
                reportDetail.setReportId(reportDao.getReportBySNO(report.getSno(), conn).getReportId());
                int rs2 = studentPaperDao.insetReportDetail(reportDetail, conn);
                if (rs1 == 1 && rs2 == 1) {
                    conn.commit();//提交事务
                    flag = true;
                } else {
                    conn.rollback();
                }
            } else {
                report.setReportId(report1.getReportId());
                reportDetail.setReportId(report1.getReportId());
                conn.setAutoCommit(false);//取消事务自动提交
                int rs1 = studentPaperDao.updateReport(report, conn);
                int rs2 = studentPaperDao.updateReportDetail(reportDetail, conn);
                if (rs1 == 1 && rs2 == 1) {
                    conn.commit();//提交事务
                    flag = true;
                } else {
                    conn.rollback();
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
        }
        return flag;
    }

    /**
     * 根据reportId查询开题报告详情
     */
    @Override
    public ReportDetail getReportDetail(Integer reportId) {
        ReportDetail reportDetail = studentPaperDao.getReportDetail(reportId, conn);
        DBUtil.closeConn(conn);
        return reportDetail;
    }

    /**
     * 根据学号查询平分
     *
     * @param sno
     */
    @Override
    public FinalGrade getScore(String sno) {
        FinalGrade finalGrade = studentPaperDao.getScore(sno, conn);
        //设置关联的指导老师成绩 评阅老师的成绩 和答辩小组的成绩
        if (finalGrade != null) {
            try {
                finalGrade.setAdviserGrade(adviserGradeDao.getAdviserGradeBySNO(finalGrade.getSno(), conn));
                finalGrade.setGrading(gradingDao.getGradingByTNO("%%", 0, 1,sno, 1, conn).get(0));
                finalGrade.setGroupGrade(groupGradeDao.getAllGroupGradeByNO("%%", 0, 1,sno, 1, conn).get(0));
                Paper paper = paperDao.getAllPaper("%%", 0, 1, sno, 1, conn).get(0);
                paper.setTitle1(titleDao.getTitleById(paper.getXtId(), conn));
                finalGrade.setPaper(paper);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        DBUtil.closeConn(conn);
        return finalGrade;
    }

    /**
     * 根据学号查询答辩信息
     */
    @Override
    public ReplyGroup getStuDefence(String sno) {
        ReplyGroup replyGroup = replyGroupDao.getStuDefence(sno, conn);
        DBUtil.closeConn(conn);
        return replyGroup;
    }
}

